package Apps;

import Dao.UntilHive;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class QueryData {
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    public void queryData(String tableName){
        String sql = "select * from " + tableName;
        System.out.println("Running: " + sql);
        try {
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                System.out.println(String.valueOf(resultSet.getInt(1)) + "\t" +
                        resultSet.getString(2) + "\t" +
                        resultSet.getString(3) + "\t" +
                        resultSet.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

//    public void countData(String tableName){
//        String sql = "select count(*) from " + tableName;
//        System.out.println("Running: " + sql);
//        try {
//            resultSet = statement.executeQuery(sql);
//        while (resultSet.next()) {
//            System.out.println(resultSet.getString(1));
//        }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//    }

    public void close(){
        UntilHive.closeAll(connection, statement, resultSet);
    }

    public static void main(String[] args){
        QueryData queryData = new QueryData();
        queryData.connection = UntilHive.getCon();
        try {
            queryData.statement = queryData.connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        queryData.queryData("u_data");
//        queryData.countData("u_data");
        queryData.close();

    }
}
